package artibella.media.controls.views 
{
	import artibella.IDisposable;
	import artibella.media.audio.AudioSpectrum;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class RTB_Spectrum extends Sprite implements IDisposable
	{
		
		public static const HORIZONTAL_BARS		:String = "horizontal";
		public static const VERTICAL_BARS		:String = "vertical";
		public static const LINKED_POINTS	 	:String = "linked";
		
		private var _signal:AudioSpectrum;
		private var _host:Sprite;
		
		private var _clr:int;
		
		private var _ecart:int = 10;
		private var _type:String;
		private var _persistence:int = 5;
		
		private var _tab:Array;
		private var _sprite:Sprite;
		
		public function RTB_Spectrum( c:int = 0x000000, type:String = RTB_Spectrum.HORIZONTAL_BARS, persistence:int = 2 ) 
		{
			_clr = c;
			_type = type;
			_persistence = persistence;
			
			_init();
		}
		
		private function _init():void
		{
			_signal = new AudioSpectrum( AudioSpectrum.LEFT );
			
			_host = new Sprite();
			addChild( _host );
			
			var s:Sprite;
			//s = new Sprite();
			//s.graphics.lineStyle( 1, _clr, .3, true );
			//s.graphics.lineTo( 226, 0 );
			//s.x = 2;
			//s.y = 39;
			//addChild( s );
			
			s = new Sprite();
			s.graphics.lineStyle( 1, _clr, .3, true );
			s.graphics.lineTo( 226, 0 );
			s.x = 2;
			s.y = 100;
			addChild( s );
		}
		
		// - - -
		
		public function play():void
		{
			if ( hasEventListener( Event.ENTER_FRAME ) ) return;
			
			addEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
		public function stop():void
		{
			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
		private function _onEnterFrame( e:Event ):void
		{
			_tab = _signal.getSpectrum();
			
			_sprite = new Sprite();
			_sprite.graphics.clear();
			_sprite.graphics.lineStyle( 2, _clr, 1, true );
			
			switch( _type )
			{
				case RTB_Spectrum.HORIZONTAL_BARS : 
					_drawHorizontalBars(); 
					break;
				case RTB_Spectrum.VERTICAL_BARS : 
					_drawVerticalBars(); 
					break;
				case RTB_Spectrum.LINKED_POINTS : 	
					_drawLinkedPoints(); 
			}
			
			_sprite.graphics.endFill();
			_sprite.alpha = .3;
			
			var m:Matrix = new Matrix();
			m.translate(0, 70 );
			
			var _bmpd:BitmapData = new BitmapData( 230, 140, true, 0x00FFFFFF );
			_bmpd.draw( _sprite, m );
			var bmp:Bitmap = new Bitmap( _bmpd );
			bmp.alpha = .3;
			
			_host.addChild( bmp );
			
			while( _host.numChildren > _persistence )
			{
				_host.removeChildAt( 0 );
			}
		}
		
		// - - -
		
		private function _drawLinkedPoints():void
		{
			for ( var i:int = 0; i < _tab.length; i ++ ) 
			{	
				if ( i == 0 ) {
					_sprite.graphics.moveTo( i * _ecart, _tab[ i ]* 50 );
				}
				else {
					_sprite.graphics.lineTo( i * _ecart, _tab[ i ]* 50 );
				}
			}
		}
		
		private function _drawVerticalBars():void
		{
			var moy:Array = new Array();
			
			for ( var j:int = 0; j < 25; j ++ ) 
			{
				moy.push( ( _tab[ ( j * 5 ) ] + _tab[ ( j * 5 ) + 1 ] + _tab [ ( j * 5 ) + 2 ] + _tab[ ( j * 5 )+ 3 ] + _tab[ ( j * 5 ) + 4 ] ) / 5 );
			}
			
			for ( var i:int = 0; i < 25; i ++ ) 
			{
				_sprite.graphics.moveTo( i * 9 + 6, 0 );
				_sprite.graphics.lineTo( ( i * 9 ) + 6, moy[ i ] * 30 );
			}
		}
		
		private function _drawHorizontalBars():void
		{
			var moy:Array = new Array();
			
			for ( var j:int = 0; j < 25; j ++ ) 
			{
				moy.push( ( _tab[ ( j * 5 ) ] + _tab[ ( j * 5 ) + 1 ] + _tab [ ( j * 5 ) + 2 ] + _tab[ ( j * 5 ) + 3 ] + _tab[ ( j * 5 ) + 4 ] ) / 5 );
			}
			
			for ( var i:int = 0; i < 25; i ++ ) 
			{
				_sprite.graphics.moveTo( i * 9, moy[ i ] * 30 );
				_sprite.graphics.lineTo( ( i * 9 ) + 9, moy[ i ] * 30 );
			}
		}
		
		
		public function dispose():void
		{
			removeEventListener( Event.ENTER_FRAME, _onEnterFrame );
		}
		
	}

}